#include "DynamicObject.h"
#include <d3d9.h>

DynamicObject::DynamicObject(void): GameObject()
{
}

DynamicObject::DynamicObject(float _posX, float _posY, float _vX, float _vY, EnumID id)
	: GameObject(_posX, _posY, id)
{	
	vX = _vX;
	vY = _vY;
	canMove = true;
}

void DynamicObject::Update(int deltaTime)
{
	posX += vX*deltaTime;
	if(posX <= 0 || posX >= G_MapWidth)
		vX = - vX;
	posY += vY*deltaTime;
	sprite->Update(deltaTime);
}

void DynamicObject::Draw(CCamera* camera)
{
	D3DXVECTOR2 center = camera->Transform(posX, posY);
	if(vX > 0)
		sprite->DrawFlipX(center.x, center.y);
	else
		sprite->Draw(center.x, center.y);
}

DynamicObject::~DynamicObject(void)
{
}

void DynamicObject::Collision(list<GameObject*> obj, int dt) 
{
	list<GameObject*>::iterator _itBegin;
	for (_itBegin = obj.begin(); _itBegin != obj.end(); _itBegin++)
	{
		float moveX;
		float moveY;
		float normalx;
		float normaly;
		GameObject* other = (*_itBegin);

		Box boxMain = this->GetBox();
		Box boxOther = other->GetBox();

		if(AABB(boxMain, boxOther, moveX, moveY) == true)
		{
			switch (other->id)
			{
			case Brick_ID:
				{
					posY += moveY;
				}
				break;
			default:
				break;
			}
		}
	}
}

Box DynamicObject::GetBox()
{
	Box result(posX - width/2 , posY- height/2, width , height, vX , vY);
	return result;
}
